package com.chen.controller;


import com.chen.annotation.OptLog;
import com.chen.common.OptTypeConst;
import com.chen.common.Result;
import com.chen.entity.Course;
import com.chen.entity.Dict;
import com.chen.mapper.CourseMapper;
import com.chen.service.impl.CourseServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author personcoder007
 * @since 2022-08-17
 */
@RestController
@RequestMapping("/course")
@Api(value = "CourseController",tags = "课程管理")
public class CourseController {


    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private CourseServiceImpl courseService;

    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有")
    public  Result findAll() {
        return  Result.success(courseMapper.selectList(null));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增或者更新")
    @OptLog(optType = OptTypeConst.SAVE_OR_UPDATE)
    public Result addDict(@RequestBody Course course){

        courseService.saveOrUpdate(course);

        return Result.success();
    }


    //根据id进行删除
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除字典")
    @OptLog(optType = OptTypeConst.REMOVE)
    public Result deleteDict(@PathVariable("id") int id){
        courseMapper.deleteById(id);
        return Result.success();
    }

    //批量删除
    @PostMapping("/del/batch")
    @ApiOperation(value = "批量删除")
    @OptLog(optType = OptTypeConst.REMOVE_BATH)
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        courseService.removeByIds(ids);

        return Result.success();
    }


    @RequestMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result selectDict(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             @RequestParam(defaultValue = "") String name){

        return Result.success(courseService.selectPage(pageNum, pageSize, name));
    }

}

